package org.dtrd.modules.doctor.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.doctor.entity.bean.WalletHistoryDetail;
import org.dtrd.modules.doctor.entity.bean.WalletHistoryListInfo;
import org.dtrd.modules.doctor.entity.bean.WalletHistoryListRequest;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorService;
import org.dtrd.modules.doctor.service.IDtrdEntRdWalletHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 医生钱包历史数据 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-20
 */
@Api(tags = "医生-钱包相关")
@RestController
@RequestMapping("/doctor/wallet")
public class WalletHistoryController extends BaseController {

    @Autowired
    private IDtrdEntRdWalletHistoryService walletHistoryService;
    @Autowired
    private IDtrdEntRdDoctorService doctorService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("验证医生钱包提现密码")
    @GetMapping("verify/pwd")
    @AutoLog(value = "验证医生钱包提现密码", operateType = OPERATE_TYPE_QUERY)
    public Result<?> verifyWalletPwd(@RequestParam("pwd") String walletPwd,
                                     @RequestParam("doctorId") Integer doctorId){
        if (StrUtil.isBlank(walletPwd) || isIntParamInvalid(doctorId)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = doctorService.verifyWallyPwd(walletPwd, doctorId);
        return result ? Result.OK() : error(STATE_VERIFY_FAILURE);
    }

    @ApiOperation(value = "查询医生钱包历史数据详情", response = WalletHistoryDetail.class)
    @GetMapping("get")
    public Result<?> getWalletHistory(@RequestParam Integer id){
        WalletHistoryDetail info = walletHistoryService.getWalletHistory(id);
        String logContent = String.format("查询医生钱包历史数据详情，查询id：%d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询医生钱包历史数据列表", response = WalletHistoryListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询医生钱包历史数据列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getWalletHistoryList(@RequestBody WalletHistoryListRequest request){
        List<WalletHistoryListInfo> list = walletHistoryService.getWalletHistoryList(request);
        String logContent = String.format("查询医生钱包历史数据列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改医生钱包历史数据")
    @PostMapping("edit")
    public Result<?> saveOrUpdateWalletHistory(@RequestBody WalletHistoryDetail dto){
        boolean result = walletHistoryService.saveOrUpdateWalletHistory(dto);
        String logContent = String.format("新增或修改医生钱包历史数据");
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除医生钱包历史数据")
    @DeleteMapping("remove")
    public Result<?> removeWalletHistory(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = walletHistoryService.removeWalletHistory(id);
        String logContent = String.format("删除医生钱包历史数据, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
